package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.PiccNurseMapper;
import org.linlinjava.litemall.db.dao.PiccPatientMeasureMapper;
import org.linlinjava.litemall.db.domain.PiccNurse;
import org.linlinjava.litemall.db.domain.PiccPatientMeasure;
import org.linlinjava.litemall.db.domain.PiccPatientMeasureExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class PiccMeasureService {
    @Resource
    private PiccPatientMeasureMapper piccPatientMeasureMapper;

    /**
     * 获取单个病人的测量list，根据病人id
     * @param patientId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<PiccPatientMeasure> getListByPatientid(Integer patientId, Integer page,
                                                       Integer limit, String sort, String order){
        PiccPatientMeasureExample example = new PiccPatientMeasureExample();
        PiccPatientMeasureExample.Criteria criteria = example.createCriteria();
        criteria.andPatientIdEqualTo(patientId);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

//        PageHelper.startPage(page, limit);
        return piccPatientMeasureMapper.selectByExample(example);
    }

    /**
     * 获取所有的病人测量报告，如果nurseId是0，则是查询所有，否则查询就byNurseId
     * @param nurseId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<PiccPatientMeasure> getAllList(Integer nurseId, Integer page, Integer limit,
                                               String sort, String order){
        PiccPatientMeasureExample example = new PiccPatientMeasureExample();
        PiccPatientMeasureExample.Criteria criteria = example.createCriteria();
        if (nurseId != 0) criteria.andNurseIdEqualTo(nurseId);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

//        PageHelper.startPage(page, limit);
        return piccPatientMeasureMapper.selectByExample(example);
    }

    /**
     * 添加新的测量报告
     * @param measure
     * @return
     */
    public PiccPatientMeasure add(@RequestBody PiccPatientMeasure measure){
        measure.setAddTime(LocalDateTime.now());
        piccPatientMeasureMapper.insertSelective(measure);
        return measure;
    }

    public int count(int nurseId) {
        PiccPatientMeasureExample example = new PiccPatientMeasureExample();
        if (nurseId > 0)
            example.or().andNurseIdEqualTo(nurseId);
        return (int) piccPatientMeasureMapper.countByExample(example);
    }
}
